#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "debug.h"
#include "ddns_proto.h"
#include "p2p_proto.h"
#include "broadcast_proto.h"
#include "zmipc_proto.h"
	
static unsigned char sendbuff[1024]={0};
static unsigned char recvbuff[1024]={0};

void test_broadcast_protocol(int sndlen,int rcvlen)
{
	int ret = 0;

	printf("struct broadcast_content size = %d\n\n",sizeof(sbroadcast_content));

	VPI_HANDLE handle = vpi_handle_init(broadcast_default_header,ROLE_DEV);
	if(handle == NULL)
		exit(EXIT_FAILURE);
	sbroadcast_content *pcontent = (sbroadcast_content*)handle->msg.content;

	handle->msg.cmd = BROADCAST_RES_STATUS;
	// fill content
	strcpy(pcontent->up_content.res_dev_status.id,"A99003001");
	strcpy(pcontent->up_content.res_dev_status.ip,"192.168.0.171");
	strcpy(pcontent->up_content.res_dev_status.subnet,"192.168.0.171");
	strcpy(pcontent->up_content.res_dev_status.gwip,"192.168.0.171");
	strcpy(pcontent->up_content.res_dev_status.dnsip,"192.168.0.171");
	strcpy(pcontent->up_content.res_dev_status.version,"1.3.32");
	pcontent->up_content.res_dev_status.web_port = 80;
	pcontent->up_content.res_dev_status.stream_port = 9300;
	strcpy(pcontent->up_content.res_dev_status.macaddr,"a3:3e:e2:r3:ew:e2");
	pcontent->up_content.res_dev_status.dhcp_flag = 1;

	fprintf(stdout,"[send content] id:%s;ip:%s;subnet:%s;gwip:%s;dnsip:%s\n"  
			"version:%s;web_port:%hu;stream_port:%hu;macaddr:%s;dhcp:%d\n",
			pcontent->up_content.res_dev_status.id,
			pcontent->up_content.res_dev_status.ip,
			pcontent->up_content.res_dev_status.subnet,
			pcontent->up_content.res_dev_status.gwip,
			pcontent->up_content.res_dev_status.dnsip,
			pcontent->up_content.res_dev_status.version,
			pcontent->up_content.res_dev_status.web_port,
			pcontent->up_content.res_dev_status.stream_port,
			pcontent->up_content.res_dev_status.macaddr,
			pcontent->up_content.res_dev_status.dhcp_flag);

	sndlen = vpi_build_package(handle,sendbuff,1024);
	printf("vpi_build_package ret = %d,sndlen = %d\n",sndlen,sndlen);

	/** exit test */
	vpi_handle_exit(handle);

	// server role
	handle = vpi_handle_init(broadcast_default_header,ROLE_SERVER);
	if(handle == NULL)
		exit(EXIT_FAILURE);
	pcontent = (sbroadcast_content*)handle->msg.content;

	memcpy(recvbuff,sendbuff,sndlen);
	rcvlen = sndlen;
	ret = vpi_parse_package(handle,recvbuff,rcvlen);
	printf("vpi_parse_package ret = %d,rcvlen = %d\n",ret,rcvlen);
	fprintf(stdout,"[recv content] id:%s;ip:%s;subnet:%s;gwip:%s;dnsip:%s\n"
			"version:%s;web_port:%hu;stream_port:%hu;macaddr:%s;dhcp:%d\n",
			pcontent->up_content.res_dev_status.id,
			pcontent->up_content.res_dev_status.ip,
			pcontent->up_content.res_dev_status.subnet,
			pcontent->up_content.res_dev_status.gwip,
			pcontent->up_content.res_dev_status.dnsip,
			pcontent->up_content.res_dev_status.version,
			pcontent->up_content.res_dev_status.web_port,
			pcontent->up_content.res_dev_status.stream_port,
			pcontent->up_content.res_dev_status.macaddr,
			pcontent->up_content.res_dev_status.dhcp_flag);

	/** exit test */
	vpi_handle_exit(handle);
}

void test_p2p_protocol(int sndlen,int rcvlen)
{
	int ret = 0;

	printf("struct p2p_content size = %d\n\n",sizeof(sp2p_content));
	
	VPI_HANDLE handle = vpi_handle_init(p2p_default_header,ROLE_DEV);
	if(handle == NULL)
		exit(EXIT_FAILURE);
	sp2p_content *pcontent = (sp2p_content *)handle->msg.content;
	
	handle->msg.cmd = P2P_NEWDEV_LOGINE;
	// fill content
	strcpy(pcontent->up_content.login.id,"A99003001");
	strcpy(pcontent->up_content.login.ip,"192.168.0.7");
	strcpy(pcontent->up_content.login.version,"1.3.23");
	pcontent->up_content.login.port = 19200;

	fprintf(stdout,"[send content] id:%s;version:%s\n",pcontent->up_content.login.id,
			pcontent->up_content.login.version);

	sndlen = vpi_build_package(handle,sendbuff,1024);
	printf("vpi_build_package ret = %d,sndlen = %d\n",sndlen,sndlen);

	/** exit test */
	vpi_handle_exit(handle);

	// server role
	handle = vpi_handle_init(p2p_default_header,ROLE_SERVER);
	if(handle == NULL)
		exit(EXIT_FAILURE);
	pcontent = (sp2p_content *)handle->msg.content;

	memcpy(recvbuff,sendbuff,sndlen);
	rcvlen = sndlen;
	ret = vpi_parse_package(handle,recvbuff,rcvlen);
	printf("vpi_parse_package ret = %d,rcvlen = %d\n",ret,rcvlen);
	fprintf(stdout,"[recv content] id:%s; version:%s\n",pcontent->up_content.login.id,
			pcontent->up_content.login.version);

	/** exit test */
	vpi_handle_exit(handle);
}

void test_ddns_protocol(int sndlen,int rcvlen)
{
	int ret = 0;
	/*
	 * DDNS protocol test
	 */
	printf("struct ddns_content size = %d\n\n",sizeof(sddns_content));

	VPI_HANDLE handle = vpi_handle_init(ddns_default_header,ROLE_DEV);
	if(handle == NULL)
		exit(EXIT_FAILURE);
	sddns_content *pcontent = (sddns_content*)handle->msg.content;

	handle->msg.cmd = CLI_UP_LOGIN;
	// fill content
	strcpy(pcontent->up_content.up_login.id,"A99003001");
	strcpy(pcontent->up_content.up_login.ip,"192.168.0.171");
	pcontent->up_content.up_login.web_port = 80;
	pcontent->up_content.up_login.stream_port = 9300;
	strcpy(pcontent->up_content.up_login.type,"IPC1101");
	strcpy(pcontent->up_content.up_login.version,"1.3.31");

	fprintf(stdout,"[SND CONTENT]id:%s;ip:%s;web:%u;stream:%u;type:%s;version:%s\n",pcontent->up_content.up_login.id,
		pcontent->up_content.up_login.ip,pcontent->up_content.up_login.web_port,
		pcontent->up_content.up_login.stream_port,pcontent->up_content.up_login.type,
		pcontent->up_content.up_login.version);

	sndlen = vpi_build_package(handle,sendbuff,1024);
	printf("vpi_build_package ret = %d,sndlen = %d\n",sndlen,sndlen);

	/** exit test */
	vpi_handle_exit(handle);

	// server role
	handle = vpi_handle_init(ddns_default_header,ROLE_SERVER);
	if(handle == NULL)
		exit(EXIT_FAILURE);
	pcontent = (sddns_content*)handle->msg.content;

	memcpy(recvbuff,sendbuff,sndlen);
	rcvlen = sndlen;
	ret = vpi_parse_package(handle,recvbuff,rcvlen);
	printf("vpi_parse_package ret = %d,rcvlen = %d\n",ret,rcvlen);
	fprintf(stdout,"[RCV CONTENT]id:%s;ip:%s;web:%u;stream:%u;type:%s;version:%s\n",pcontent->up_content.up_login.id,
		pcontent->up_content.up_login.ip,pcontent->up_content.up_login.web_port,
		pcontent->up_content.up_login.stream_port,pcontent->up_content.up_login.type,
		pcontent->up_content.up_login.version);
	/** exit test */
	vpi_handle_exit(handle);
}

void test_zmipc_protocol(int sndlen, int rcvlen)
{
    int ret = 0;

	printf("struct zmipc_content size = %d\n\n",sizeof(szmipc_content));

	// device role
	VPI_HANDLE handle = vpi_handle_init(zmipc_default_header,ROLE_DEV);
	if(handle == NULL)
		exit(EXIT_FAILURE);
	szmipc_content *pcontent = (szmipc_content *)handle->msg.content;

    handle->msg.cmd = SOCKET_RE_CLIENT_LOGIN;
	struct up_data_transfer *pdata = &pcontent->up_content.data_transfer;
	pdata->res_login.status = ZMIPC_RES_OK;
	pdata->res_login.frate = 15;
	pdata->res_login.audio_enc = ZMIPC_AENC_G711A;
	pdata->res_login.brate = 512;
	pdata->res_login.cbr = ZMIPC_CBR;
	pdata->res_login.video_enc = ZMIPC_VENC_H264;
    fprintf(stdout, "[send content] status:%d,frate:%d,aenc:%d,brate:%d,cbr:%d,venc:%d\n", 
		pdata->res_login.status,pdata->res_login.frate,pdata->res_login.audio_enc,pdata->res_login.brate ,pdata->res_login.cbr ,pdata->res_login.video_enc);

    sndlen = vpi_build_package(handle, sendbuff, 1024);
    printf("vpi_build_package ret = %d,sndlen = %d\n", sndlen, sndlen);

	/** Test Re Network Get */
	memset(recvbuff,0,sizeof(recvbuff));
	recvbuff[0]=0x40;
	recvbuff[1]=0x21;
	recvbuff[2]=0x08;
	recvbuff[3]=0x00;
	recvbuff[4]=0xa2;
	recvbuff[5]=0x01;
	recvbuff[6]=0x0c;
	recvbuff[7]=0x23;
	ret = vpi_parse_package(handle,recvbuff,8);
	fprintf(stdout,"ret = %d\n",ret);

    handle->msg.cmd = SOCKET_RE_NET;
	struct network_params_operation *pnet= &pcontent->up_content.res_network_params;
	pnet->up_res_status = ZMIPC_RES_SUCCESS;

	strcpy(pnet->ip,"192.168.0.178");
	strcpy(pnet->mask,"255.255.255.0");
	strcpy(pnet->gateway,"192.168.0.1");
	strcpy(pnet->dns1,"202.112.20.131");
	strcpy(pnet->dns2,"192.168.0.1");

	pnet->web_port = 80;
	pnet->video_port = 9300;
	pnet->rtsp_port = 8554;
	pnet->upnp_flag = ZMIPC_FLAG_OFF;
	pnet->dhcp_flag = ZMIPC_FLAG_ON;

	fprintf(stdout,"ip:%s,mask:%s,gw:%s,fdns:%s,sdns:%s\n",
			pnet->ip,pnet->mask,pnet->gateway,pnet->dns1,pnet->dns2);

    sndlen = vpi_build_package(handle, sendbuff, 1024);
    printf("vpi_build_package ret = %d,sndlen = %d\n", sndlen, sndlen);
	/** End Re Network */

	// server role
    handle->role = ROLE_SERVER;
    handle->msg.cmd = SOCKET_CLIENT_LOGIN;
	memset(pcontent,0,sizeof(szmipc_content));
	struct down_data_transfer *pdown_data = &pcontent->down_content.data_transfer;
	strcpy(pdown_data->login_passwd.password, "1234567890");
	pdown_data->login_passwd.len = 10;
    fprintf(stdout, "[server send login] password:%s,len:%d\n",pdown_data->login_passwd.password,pdown_data->login_passwd.len); 
	sndlen = vpi_build_package(handle,sendbuff,1024);

	// device role
    handle->role = ROLE_DEV;
    handle->msg.cmd = 0;
	memset(pcontent,0,sizeof(szmipc_content));
    memcpy(recvbuff, sendbuff, sndlen);
    rcvlen = sndlen;
    ret = vpi_parse_package(handle, recvbuff, rcvlen);
    printf("vpi_parse_package ret = %d,rcvlen = %d\n", ret, rcvlen);
    fprintf(stdout, "[recv content] password:%s; len:%d\n", 
		pdown_data->login_passwd.password,pdown_data->login_passwd.len);

	/** exit test */
	vpi_handle_exit(handle);
}

int main(int argc,char *argv[])
{
	int sndlen = 1024,rcvlen = 1024;
	printf("sproto_package size = %d\n",sizeof(sproto_package));
	// unsigned char tmp = 0xa4;

	// printf("0x%02x\n",tmp); 
	puts("\ntest ddns api:");
	sndlen = 1024,rcvlen = 1024;
	test_ddns_protocol(sndlen,rcvlen);

	puts("\ntest p2p api:");
	sndlen = 1024,rcvlen = 1024;
	test_p2p_protocol(sndlen,rcvlen);

	puts("\ntest broadcast api:");
	sndlen = 1024,rcvlen = 1024;
	test_broadcast_protocol(sndlen,rcvlen);

	puts("\ntest zmipc api:");
	sndlen = 1024,rcvlen = 1024;
	test_zmipc_protocol(sndlen,rcvlen);

	return 0;
}
